package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fcgzs.springboot.mapper.OrdersettingMapper;
import com.fcgzs.springboot.pojo.Ordersetting;
import com.fcgzs.springboot.service.OrdersettingService;
import com.fcgzs.springboot.util.MessageConstant;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Resource
    private OrdersettingMapper ordersettingMapper;
    //保存预约信息
    @Override
    public Result saveOrdersettingInfo(List<String[]> strings) {
        boolean flag = false;
        Result result = null;
        for(String [] info :strings){
            // [年-月-日，可预约人数]
            Ordersetting ordersetting = new Ordersetting();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            LocalDate date = LocalDate.parse(info[0], dateTimeFormatter); //格式转换
            ordersetting.setOrderdate(date); //预约日期
            ordersetting.setNumber(Integer.parseInt(info[1])); //可预约人数
            //查询数据库是否存在该日期预约信息
            QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("orderDate",date); //预约日期
            Ordersetting ordersetting1 = ordersettingMapper.selectOne(queryWrapper);
            if(ordersetting1 != null){
                //有则更新
                ordersetting.setNumber(Integer.parseInt(info[1]));
                flag = ordersettingMapper.updateById(ordersetting1) > 0;
            }else {
                //无则添加
                flag = ordersettingMapper.insert(ordersetting) > 0;
            }
        }
        if(flag){
            return result = new Result(flag, MessageConstant.ADD_ORDERSETTING_SUCCESS);
        }else {
            return result = new Result(flag, MessageConstant.ADD_ORDERSETTING_FAIL);
        }
    }
    //展示预约信息
    @Override
    public Result showOrdersetting(String date) {
        String startDate = date + "-01"; //表示2021-10-01，月初
        String endDate = date + "-31"; //表示2021-10-31，月末
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("orderDate",startDate,endDate);
        List<Ordersetting> ordersettings = ordersettingMapper.selectList(queryWrapper);
        List<Map<String, Integer>> list = new ArrayList<>();
        for(Ordersetting one : ordersettings){
            Map<String, Integer> map = new HashMap<>();
            map.put("date",one.getOrderdate().getDayOfMonth());
            map.put("number",one.getNumber());
            map.put("reservations",one.getReservations());
            list.add(map);
        }
        return new Result(true,"",list);
    }
    //修改预约信息
    @Override
    public Result updateNumber(Ordersetting ordersetting) {
        boolean flag = false;
        Result result = null;
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderDate",ordersetting.getOrderdate());
        Ordersetting ordersetting1 = ordersettingMapper.selectOne(queryWrapper);
        if(ordersetting1 != null){ //更新
            if(ordersetting.getNumber() >= ordersetting1.getReservations()){ //新可预约人数大于已预约人数
                flag = ordersettingMapper.update(ordersetting,queryWrapper) > 0;
            }else {
                return result = new Result(flag,MessageConstant.UPDATE_ORDERSETTING_FAIL);
            }
        }else { //添加
            flag = ordersettingMapper.insert(ordersetting) > 0;
        }
        if(flag){
            return result = new Result(flag, MessageConstant.UPDATE_ORDERSETTING_SUCCESS);
        }else {
            return result = new Result(flag, MessageConstant.UPDATE_ORDERSETTING_FAIL);
        }
    }
}
